from PyQt5.QtWidgets import QWidget, QMessageBox

# 导入窗体内容
from design.shareDlg import *

import pyperclip
import time

from common.logger import logger
from common.retcode import RetCode
from common.utils import center_window_on_screen
from api.share import ShareAction

# 创建 MainWin 类并传入Ui_MainWindow
class ShareDlg(QWidget, Ui_ShareDlg):
	def __init__(self, parent=None):
		super(ShareDlg, self).__init__(parent)
		self.setupUi(self)
		center_window_on_screen(self)

		self._sess_wp = None
		self._cur_file = None

		self.initComboBox()

		self.toolButtonCopyUrl.clicked.connect(self.onClickedCopyUrl)
		self.toolButtonCopyPwd.clicked.connect(self.onClickedCopyPwd)

		self.pushButtonShare.clicked.connect(self.onClickedShare)

	def initComboBox(self):
		self.comboBoxExpireTime.insertItem(0, '1天', 1)
		self.comboBoxExpireTime.insertItem(1, '7天', 2)
		self.comboBoxExpireTime.insertItem(2, '永久', 3)
		self.comboBoxExpireTime.setCurrentIndex(1)

		self.comboBoxWithAccessCode.insertItem(0, '私密分享', 1)
		self.comboBoxWithAccessCode.insertItem(1, '公开分享', 2)
		self.comboBoxWithAccessCode.setCurrentIndex(0)

	def reset(self, sess_wp, cur_file):
		self._sess_wp = sess_wp
		self._cur_file = cur_file

		self.labelShareUrl.setText('未知链接')
		self.labelSharePwd.setText('未知密码')
		self.pushButtonShare.setEnabled(True)

	def onClickedCopyUrl(self):
		pyperclip.copy(self.labelShareUrl.text())
		QMessageBox.information(self, "提示", "已复制分享链接")

	def onClickedCopyPwd(self):
		pyperclip.copy(self.labelSharePwd.text())
		QMessageBox.information(self, "提示", "已复制分享密码")

	def onClickedShare(self):
		expireTime = self.comboBoxExpireTime.currentData()
		withAccessCode = self.comboBoxWithAccessCode.currentData()
		# print(expireTime, withAccessCode)

		share_action = ShareAction(self._sess_wp)
		code, share_info = share_action.share_file(self._cur_file.id, expireTime, withAccessCode)
		if code == RetCode.SUCCESS:
			print(share_info)
			share_url, pwd = share_info
			self.labelShareUrl.setText(share_url)
			self.labelSharePwd.setText(pwd)
		else:
			QMessageBox.information(self, "提示", "分享失败")